package com.dandan.mobilelover.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.dandan.mobilelover.component.Pagination;
import com.dandan.mobilelover.entity.Book;
import com.dandan.mobilelover.entity.Category;
import com.dandan.mobilelover.entity.User;
import com.dandan.mobilelover.service.BookService;
import com.dandan.mobilelover.service.CategoryService;

@Controller
@RequestMapping("/data_mgr")
@SessionAttributes("currentUser")
public class BookManageController {
	@Inject
	private BookService bookService;
	
	@Inject
	private CategoryService categoryService;
	
	@Inject
	private Pagination page;
	
	@RequestMapping(value="/book", method= RequestMethod.GET)
	public String book_import(HttpServletRequest request, Model model){
		if(request.getParameter("p") == null){
			page.setCurrentPage(1);
		}else{
			page.setCurrentPage(Integer.parseInt(request.getParameter("p")));
		}
		List<Book> books = bookService.searchList(page);
		page.setBaseUrl(request.getRequestURI());
		model.addAttribute("book_list", books);
		model.addAttribute("page", page);
		return "data_import/book/book_import";
	}
	
	@RequestMapping(value="/book/new", method=RequestMethod.GET)
	public String new_book(Model model){
		List<Category> categories = categoryService.findByType(Category.TYPE[0]);
		model.addAttribute("categories", categories);
		return "data_import/book/new";
	}
	
	@RequestMapping(value="/book/create", method=RequestMethod.POST)
	public String create_book(Book book,@RequestParam("book_file") MultipartFile book_file, int category_id,HttpServletRequest request,  Model model, @ModelAttribute("currentUser") User user ){
		System.out.println(book);
		System.out.println(user);
		System.out.println(category_id);
		String path = request.getRealPath("/uploads");
		Date date = new Date();
		String url = "/uploads/books/" + date.getTime() + "/" +  book_file.getOriginalFilename();
		path = path +  File.separator +"books" + File.separator + date.getTime()  + File.separator;
		try {
			System.out.println(path +    book_file.getOriginalFilename());
			new File(path).mkdirs();
			FileCopyUtils.copy(book_file.getBytes(), new File(path + book_file.getOriginalFilename()));
			book.setUrl(url);
			book.setUser(user);
			book.setCategory(categoryService.findById(category_id));
			bookService.saveBook(book);
		} catch (IOException e) {
			e.printStackTrace();
			return "redirect:/data_mgr/book/new";
		}
//		bookService.saveBook(book);
		return "redirect:/data_mgr/book";
	}
	
 
}
